var util = require('../../utils/util.js');
var api = require('../../config/api.js');

var app = getApp()
Page({
  data: {
    keyword: '',
    searchStatus: false,
    goodsList: [],
    historyKeyword: [],
    sortField: 'modifyTime',
    sortType: 'modifyTime',
    sortOrder: 'desc',
    defaultKeyword: {},
    hotKeyword: [],
    page: 1,
    limit: 10
  },
  //事件处理函数
  closeSearch: function () {
    wx.navigateBack()
  },
  //取消按钮
  clearKeyword: function () {
    this.setData({
      keyword: '',
      searchStatus: false
    });
  },
  onLoad: function () {
    // 页面显示
    var that = this;

    that.getSearchKeyword();
  },
  //显示热门搜索
  getSearchKeyword() {
    let that = this;
    util.request(api.SearchTiaojian).then(function (res) {
      if (res.result == 1) {
        that.setData({
          hotKeyword: res.data.search,
          historyKeyword: res.data.history
        });
        if (res.data.search.length > 0) {
          that.setData({
            defaultKeyword: res.data.search[0]
          });
        }
      }
    });
  },
  //搜索框改变值
  inputChange: function (e) {
    this.setData({
      keyword: e.detail.value,
      searchStatus: false
    });
  },
  //清空搜索框
  delinput: function (e) {
    this.setData({
      keyword: ''
    });
  },
  //搜索框聚焦
  inputFocus: function (e) {
    this.setData({
      keyword: e.detail.value,
      searchStatus: false
    });
  },
  openSortFilter: function (event) {
    let currentId = event.currentTarget.id;
    switch (currentId) {
      case 'modifyTimeSort':
        //综合排序
        this.setData({
          sortType: 'modifyTime',
          sortField: 'modifyTime',
          sortOrder: 'desc',
          page: 1,
          goodsList: []
        });
        this.getGoodsList();
        break;
      case 'priceSort':
        //价格排序
        let tmpSortOrder = 'asc';
        if (this.data.sortOrder == 'asc') {
          tmpSortOrder = 'desc';
        }
        this.setData({
          sortType: 'price',
          sortField: 'xiaoshoujia',
          sortOrder: tmpSortOrder,
          page: 1,
          goodsList: []
        });
        this.getGoodsList();
        break;
      case 'xiaoshounumSort':
        //销量排序
        this.setData({
          sortType: 'xiaoshounum',
          sortField: 'xiaoshounum',
          sortOrder: 'desc',
          page: 1,
          goodsList: []
        });
        this.getGoodsList();
        break;
    }
  },
  //清除历史搜索
  clearHistory: function () {
    let that = this;
    this.setData({
      historyKeyword: []
    })
    util.request(api.SearchClearHistory)
      .then(function (res) {
        console.log('清除成功');
      });
  },
  //搜索商品
  getGoodsList: function () {
    let that = this;
    util.request(api.SearchGoodsList, {
      searchname: that.data.keyword,
      sortField: that.data.sortField,
      sortOrder: that.data.sortOrder,
      page: that.data.page ,
      limit: that.data.limit
    }).then(function (res) {
      if (res.result == 1) {
        if (res.data.length > 0) {
          that.setData({
            searchStatus: true,
            goodsList: that.data.goodsList.concat(res.data),
            page: that.data.page + 1
          });
        } else {
          if (that.data.length > 0) {
            wx.showToast({
              title: '已经被你看完了!',
              icon: 'none',
              duration: 2000
            });
            return false;
          } else {
            that.setData({
              searchStatus: true
            });
          }
        }
      }
      //重新获取关键词
      that.getSearchKeyword();
    });
  },
  //搜索回车键
  onKeywordTap: function (event) {
    this.getSearchResult(event.target.dataset.keyword);
  },
  //搜索结果
  getSearchResult(keyword) {
    if (keyword === '') {
      keyword = this.data.defaultKeyword.keyword;
    }
    this.setData({
      keyword: keyword,
      page: 1,
      goodsList: []
    });
    this.getGoodsList();
  },
  //搜索确认
  onKeywordConfirm(event) {
    this.getSearchResult(event.detail.value);
  },
  //下拉触底
  onReachBottom: function () {
    this.getGoodsList();
  }
})